﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

/// <summary>
/// 对队列的扩展
/// </summary>
public static class QueueExtend
{
    /// <summary>
    /// 无条件将内容入栈
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="queue"></param>
    /// <param name="range"></param>
    /// <returns>自身</returns>
    public static Queue<T> EnqueueRange<T>(this Queue<T> queue, IEnumerable<T> range)
    {
        foreach (var item in range)
        {
            queue.Enqueue(item);
        }

        return queue;
    }

    /// <summary>
    /// 如果这个队列是空的话，就将一组内容入栈。否则不入栈
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="queue"></param>
    /// <param name="range"></param>
    /// <param name="callback">参数为是否入栈成功</param>
    /// <returns>入栈成功？</returns>
    public static Queue<T> EnqueueRangeIfEmpty<T>(this Queue<T> queue, IEnumerable<T> range, Action<bool> callback = null)
    {
        if (queue.Count > 0)
        {
            callback?.Invoke(false);
            return queue;
        }
        queue.EnqueueRange<T>(range);

        callback?.Invoke(true);
        return queue;
    }
}

